var a = getApp();

Page({
  data: {
    zgzimgurl1: "", //医师资格证1
    zgzimgurl2: "", //医师资格证2
    zczimgurl: "", //医师职称证
    sfzimgurl1: "", //身份证1
    sfzimgurl2: "", //身份证2
    gzzimgurl: "", //工作证
    zkbianhao: "", //资格证编号
    sfzbianhao: "", //身份证编号
    zgzimgurl1back: "", //医师资格证1
    zgzimgurl2back: "", //医师资格证2
    zczimgurlback: "", //医师职称证
    sfzimgurl1back: "", //身份证1
    sfzimgurl2back: "", //身份证2
    advertisement: "", //工作证
    img: "",
    WGender: [],
    WGenderID: [],
    WGendertext: "",
    z_content:"",
    z_telephone:"",
    WGenderIDID: "",
    Whospital: "",
    office: "",
    Wclasses: ["医生", "护士"],
    WclassesText: "",
    jobType: "",
    WpostID: [],
    z_zhicheng: "",
    z_zhichengID: "",
    bgGoodAt: "",
    Wcertificate: "请完善",
    name: "",
    identityCard: "",
    show: !0,
    show1: !0,
    briefIntroduce: "",
    num: 0,
    numText: "已认证",
    array: ['女', '男'], //性别
    index: 0, //性别
    lt_array:['否','是'],
    lt_index:0,
    is_green:0,
    pz_array:['否','是'],
    pz_index:0,
    is_examine:0,
    jj_array:['否','是'],
    jj_index:0,
    is_urgent:0,
    picc_array:['否','是'],
    picc_index:0,
    is_picc:0,
    arrays: ['综合医院', '中医医院', '中西医结合医院', '民族医医院', '专科医院', '康复医院', '妇幼保健院', '中心卫生院', '乡（镇）卫生院', '街道卫生院', '疗养院'], //类别
    indexs: 0, //类别
    title: [], //职称
    index2: 0, //职称
    disease: ['感冒', '接骨'], //擅长疾病
    index3: 0, //擅长疾病
    IMG1: '/assets/images/head.png',
    IMG2: '/assets/images/head.png',
    z_sex: 1,
    zid: 0,
    fenleiList:[
      {
        id:1,
        name:'医院'
      },
      {
        id:2,
        name:'药房'
      },
      {
        id:3,
        name:'体检机构'
      },
      {
        id:4,
        name:'诊所'
      }
    ]
  },
  zkbianhao: function (e) {
    this.setData({
      zkbianhao: e.detail.value
    })
  },
  sfzbianhao: function (e) {
    this.setData({
      sfzbianhao: e.detail.value
    })
  },
  name: function (e) {
    let val = e.detail.value;
    this.setData({
      name: val
    })
  },
  input: function (e) {
    console.log(e)
    var that = this
    var authority = e.detail.value
    that.setData({
      authority: authority
    })
  },

  //获取个人简介
  info_input:function(e){
    console.log(e)
    var z_content=e.detail.value;
    this.setData({
      z_content:z_content
    })
  },

  //更换医生头像
  replace: function () {
    var that = this;
    var uniacid = a.siteInfo.uniacid;
    var zhuanjiaImg = that.data.zhuanjiaImg;
    var openid = wx.getStorageSync('openid')
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var imgUrl = res.tempFilePaths[0];
        //console.log(tempFilePaths);
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=uploadback&m=hyb_yl',
          filePath: imgUrl,
          name: 'upfile',
          formData: {
            'path': 'wxchat'
          },
          success: function (res) {
            console.log(res.data)
            that.setData({
              uplogo: res.data,
              z_thumbs: imgUrl
            })
          },
          fail: function (res) {
            console.log(res)
          },
        })
        that.setData({
          logo: imgUrl
        })
      }
    })
  },
  //性别
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e)
    if (e.detail.value == 0) {
      var z_sex = 0
    } else {
      var z_sex = 1
    }
    let array = this.data.array[e.detail.value];
    this.setData({
      index: e.detail.value,
      WGendertext: array,
      z_sex: z_sex
    })
  },
  //是否绿通
  bindlvtongChange: function (e) {
    if (e.detail.value == 0) {
      var is_green = 0
    } else {
      var is_green = 1
    }
    this.setData({
      lt_index: e.detail.value,
      is_green: is_green
    })
  },
  //是否陪诊
  bindpzChange: function (e) {
    if (e.detail.value == 0) {
      var is_examine = 0
    } else {
      var is_examine = 1
    }
    this.setData({
      pz_index: e.detail.value,
      is_examine: is_examine
    })
  },
   //是否接受PICC护理服务
   bindpiccChange: function (e) {
    if (e.detail.value == 0) {
      var is_picc = 0
    } else {
      var is_picc = 1
    }
    this.setData({
      picc_index: e.detail.value,
      is_picc: is_picc
    })
  },
  //是否加急
  bindjjChange: function (e) {
    if (e.detail.value == 0) {
      var is_urgent = 0
    } else {
      var is_urgent = 1
    }
    this.setData({
      jj_index: e.detail.value,
      is_urgent: is_urgent
    })
  },
  //分组
  chosefenlei: function (e) {
    console.log(e)
    var that = this
    var fenleiList = this.data.fenleiList
    var qx_id = this.data.fenleiList[e.detail.value].id; //职称id
    var fenleiIndex = e.detail.value
    this.setData({
      fenleiIndex: e.detail.value,
      qx_id: qx_id,
      fqname:fenleiList[fenleiIndex].name
    })
  },
  //类别
  bindPickerChange2: function (e) {
    let arrays = this.data.arrays[e.detail.value];
    this.setData({
      indexs: e.detail.value,
      WclassesText: arrays
    })
  },
  //职称
  bindPickerChange3: function (e) {
    console.log(e)
    let title = this.data.title[e.detail.value].job_name;
    var z_zhicheng = this.data.title[e.detail.value].id; //职称id
    console.log(z_zhicheng)
    this.setData({
      index2: e.detail.value,
      z_zhicheng: z_zhicheng
    })
  },
  //擅长疾病
  bindPickerChange4: function (e) {
    let title = this.data.title[e.detail.value];
    this.setData({
      index3: e.detail.value,
      bgGoodAt: title
    })
  },
  //身份证
  identityCard: function (e) {
    console.log(e)
    let val = e.detail.value;
    let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(val) === false) {
      wx.showToast({
        title: "身份证错误",
        icon: "none",
        duration: 2e3
      })
    } else {
      this.setData({
        sfzbianhao: val
      })
    }
  },
  imgQualifications: function () {
    let that = this;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        console.log(res)
        let tempFilePaths = res.tempFilePaths[0];
        that.setData({
          IMG1: tempFilePaths
        })
      }
    })
  },
  uploadimgs(e) {
    var uniacid = a.siteInfo.uniacid;
    let t = this,
      m = e.currentTarget.dataset.mnames;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        let tempFilePaths = res.tempFilePaths[0];
        wx.uploadFile({
          url: t.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {},
          success: function (res) {
            console.log(res)
            "zgzimgurl1" == m ? t.setData({

              zgzimgurl1: res.data,
            }) : "zgzimgurl2" == m ? t.setData({
              zgzimgurl2: res.data,
            }) : "zczimgurl" == m ? t.setData({
              zczimgurl: res.data,
            }) : "sfzimgurl1" == m ? t.setData({
              sfzimgurl1: res.data,
            }) : "sfzimgurl2" == m ? t.setData({
              sfzimgurl2: res.data,
            }) : "gzzimgurl" == m ? t.setData({
              gzzimgurl:  res.data,
            }) : "advertisement" == m ? t.setData({
              advertisement: res.data,
            }) : console.log(m);
          },
          fail: function (res) {
            console.log(res)
          },
        })
      }
    })
  },
  imgQualifications2: function () {
    let that = this;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        console.log(res)
        let tempFilePaths = res.tempFilePaths[0];
        that.setData({
          IMG2: tempFilePaths
        })
      }
    })
  },
  z_telephone:function(e){
    var that = this;
    that.setData({
      z_telephone:e.detail.value
    })
  },

  // getPhoneNumber: function (e) {
  //   console.log(e)
  //   var that = this
  //   var errMsg = e.detail.errMsg
  //   var iv = e.detail.iv
  //   var encryptedData = e.detail.encryptedData
  //   var code = wx.getStorageSync('code')
  //   var sessionKey = wx.getStorageSync('sessionKey')
  //   console.log(sessionKey, code)
  //   if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  //     wx.showModal({
  //       title: '提示',
  //       showCancel: false,
  //       content: '未授权',
  //       success: function (res) {}
  //     })
  //   } else {
  //     a.util.request({
  //       'url': 'entry/wxapp/user.mdpwd',
  //       data: {
  //         iv: e.detail.iv,
  //         code: code,
  //         sessionKey: sessionKey,
  //         encryptedData: e.detail.encryptedData
  //       },
  //       success: function (res) {
  //         console.log(res)
  //         if (res.data.gstage == '1') {
  //           var rdata = JSON.parse(res.data.rdata);
  //           console.log(rdata)
  //           var u_phone = rdata.phoneNumber
  //           //更新数据
  //           a.util.request({
  //             'url': 'entry/wxapp/user.updatephone',
  //             data: {
  //               openid: wx.getStorageSync('openid'),
  //               u_phone: u_phone
  //             },
  //             success: function (res) {
  //               console.log(res)
  //             }
  //           })
  //           that.setData({
  //             z_telephone: rdata.phoneNumber
  //           });
  //           wx.showToast({
  //             title: '绑定手机号码成功!',
  //             icon: 'none',
  //           });
  //         }
  //       },
  //     })
  //   }
  // },

  Wimg: function (t) {
    var e = this;
    e.setData({
      num: 0,
      numText: "保存"
    }), wx.chooseImage({
      count: 1,
      sizeType: ["orignal", "compressed"],
      sourceType: ["album", "camera"],
      success: function (t) {
        console.log(t.tempFilePaths[0]), e.setData({
          z_thumbs: t.tempFilePaths[0],
          show: !1
        });
        var o = wx.getStorageSync("log") || "";
      }
    });
  },
  Whospital: function (a) {
    var qx_id = this.data.qx_id;
    if(qx_id == '')
    {
      wx.showToast({
        title: '请先选择所属分组',
      })
    }else{
      this.setData({
        num: 0,
        numText: "保存"
      }), wx.setStorageSync("url", "doctor/info/update/"), wx.navigateTo({
        url: "/hyb_yl/backstageLifes/pages/WapproveWhospital/WapproveWhospital?qx_id="+qx_id
      });
    }
    
  },
  WadministrativeOffice: function (e) {
    var hid = e.currentTarget.dataset.hid
    console.log(hid)
    if (!hid) {
      wx.showToast({
        title: "请先选择医院",
        icon: "none",
        duration: 2e3
      })
    } else {
      wx.navigateTo({
        url: "/hyb_yl/backstageLifes/pages/WapproveWhospitaloffice/WapproveWhospitaloffice?hid=" + hid
      });
    }

  },
  z_name: function (e) {
    console.log(e)
    var that = this
    that.setData({
      z_name: e.detail.value
    })
  },
  bgGoodAt: function (a) {
    this.setData({
      num: 0,
      numText: "保存"
    }), wx.setStorageSync("url", "doctor/info/update/");
    var t = this;
    "" != t.data.jobType ? (wx.setStorageSync("jobType", t.data.jobType), wx.navigateTo({
      url: "/hyb_yl/index/WapproveJ/WapproveJ"
    })) : "" == t.data.jobType && wx.showModal({
      title: "提示",
      content: "请选着类别",
      success: function (a) {
        a.confirm ? console.log("用户点击确定") : a.cancel && console.log("用户点击取消");
      }
    });
  },

  WTT: function (e) {
    let that = this;
    var zid = that.data.zid
    var sfzbianhao = that.data.sfzbianhao //身份证
    var z_telephone = that.data.z_telephone //电话
    var z_content = that.data.z_content //简介
    var z_yiyuan = that.data.agentname
    var name = that.data.name
    var z_sex = that.data.z_sex
    var z_room = that.data.z_room
    var qx_id = that.data.qx_id
    console.log(qx_id)
    var hid = that.data.hid
    var z_name = that.data.z_name
    var authority = that.data.authority
    var zgzimgurl1back = that.data.zgzimgurl1; //医师资格证1
    var zgzimgurl2back = that.data.zgzimgurl2back; //医师资格证2
    var zczimgurlback = that.data.zczimgurlback; //医师职称证
    var sfzimgurl1back = that.data.sfzimgurl1; //身份证1
    var sfzimgurl2back = that.data.sfzimgurl2; //身份证2
    var advertisement = that.data.advertisement; //工作证
    var zkbianhao = that.data.zkbianhao; //资格证编号
    var sfzbianhao = that.data.sfzbianhao; //身份证编号
    var advertisement = that.data.advertisement //头像
    var z_zhicheng = that.data.z_zhicheng //职称
    var parentid = that.data.parentid //二级科室
    var address = that.data.address
    var lng = that.data.lng
    var lat = that.data.lat
    var is_green = that.data.is_green
    var is_examine = that.data.is_examine;
    var is_urgent = that.data.is_urgent;

    if (!that.data.uplogo) {
      var z_thumbs = that.data.z_thumbsback
    } else {
      var z_thumbs = that.data.uplogo
    }
    if (z_name == "") {
      wx.showToast({
        title: "姓名不能为空",
        icon: "none",
        duration: 2e3
      })
    } else if (sfzbianhao == "" || sfzbianhao == undefined) {
      wx.showToast({
        title: "请填写身份证",
        icon: "none",
        duration: 2e3
      })
    } else if (z_yiyuan == "") {
      wx.showToast({
        title: "请选择医院",
        icon: "none",
        duration: 2e3
      })
    } else if (z_zhicheng == "") {
      wx.showToast({
        title: "请选择职称",
        icon: "none",
        duration: 2e3
      })
    } else if (authority == "" || authority == undefined) {
      wx.showToast({
        title: "擅长疾病不能为空",
        icon: "none",
        duration: 2e3
      })
    } 
    else if (z_content == "" || z_content == undefined) {
      wx.showToast({
        title: "请填写个人简介",
        icon: "none",
        duration: 2e3
      })
    } else if (zgzimgurl1back == "") {
      wx.showToast({
        title: "请上传证件照",
        icon: "none",
        duration: 2e3
      })
    } 
    else if (sfzbianhao == "") {
      wx.showToast({
        title: "请填写身份证号",
        icon: "none",
        duration: 2e3
      })
    } 
    else if (sfzimgurl1back == "") {
      wx.showToast({
        title: "请上传身份证正面",
        icon: "none",
        duration: 2e3
      })
    } else if (sfzimgurl2back == "") {
      wx.showToast({
        title: "请上传身份证反面",
        icon: "none",
        duration: 2e3
      })
    } else if (advertisement == "") {
      wx.showToast({
        title: "请上传工作证",
        icon: "none",
        duration: 2e3
      })
    } 
    else {
      wx.showModal({
        title: '提示',
        content: ' 确认提交么？ ',
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定');
            var that = this;
            var values = e.detail.value;
            a.util.request({
              url: 'entry/wxapp/regin.zhuce',
              data: {
                address: address,
                lng: lng,
                lat: lat,
                zid: zid,
                z_telephone: z_telephone, //电话
                z_content: z_content, //简介
                z_sex: z_sex,
                z_room: z_room, //一级科室
                parentid: parentid,
                hid: hid,
                z_zhicheng: z_zhicheng, //职称
                z_name: z_name,
                openid: wx.getStorageSync('openid'),
                zgzimgurl1back: zgzimgurl1back, //医师资格证1
                sfzimgurl1back: sfzimgurl1back, //身份证1
                sfzimgurl2back: sfzimgurl2back, //身份证2
                advertisement: advertisement, //工作证
                sfzbianhao: sfzbianhao, //身份证编号
                authority: authority,
                qx_id: qx_id,
                is_urgent:is_urgent,
                is_examine:is_examine,
                is_green:is_green
              },
              success: function (res) {
                console.log(res)
                wx.showToast({
                  title: "提交成功",
                  icon: "none",
                  duration: 2e3,
                  success: function () {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      });
                    }, 2000)
                  }
                })
              },
              fail: function (err) {
                console.log(err);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    }

  },
  onLoad: function (t) {
    var that = this
    var zid = t.zid ? t.zid : 0
    if (t.numText) {
      var numText = t.numText
      that.setData({
        numText: numText
      })
    }
    a.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        that.setData({
          url: res.data,
          imgurl: res.data + "attachment/"
        })
      },
    })
    a.util.request({
      'url': 'entry/wxapp/regin.zjinfo',
      'data': {
        id: zid
      },
      success: function (res) {
        console.log(res.data.jigouqx.name)
        var z_sex = res.data.z_sex
        if (z_sex == 0) {
          var index = '女'
        }
        if (z_sex == 1) {
          var index = '男'
        }
        var id = res.data.z_room
        that.setData({
          address: res.data.address,
          userinfo: res.data,
          index: index,
          touxiangurl: res.data.z_thumbs,
          z_thumbs: res.data.z_thumbs,
          z_thumbsback: res.data.z_thumbsback,
          z_name: res.data.z_name,
          z_telephone: res.data.z_telephone,
          agentname: res.data.agentname,
          name: res.data.name,
          z_content: res.data.z_content,
          z_thumb: res.data.z_thumb,
          hid: res.data.hid,
          z_room: res.data.z_room,
          z_zhicheng: res.data.z_zhicheng,
          authority: res.data.authority,
          sfzbianhao: res.data.sfzbianhao,
          zgzimgurl1: res.data.zgzimgurl1back, //医师资格证1
          zgzimgurl2: res.data.zgzimgurl2back, //医师资格证2
          zczimgurl: res.data.zczimgurlback, //医师职称证
          sfzimgurl1: res.data.sfzimgurl1back, //身份证1
          sfzimgurl2: res.data.sfzimgurl2back, //身份证2
          advertisement: res.data.advertisement, //工作证
          zkbianhao: res.data.zkbianhao, //资格证编号
          sfzbianhao: res.data.sfzbianhao, //身份证编号
          exa: res.data.exa,
          parentid: res.data.parentid,
          z_sex: res.data.z_sex,
          qx_id: res.data.qx_id,
          fqname: res.data.jigouqx.name,
          is_green:res.data.is_green,
          is_examine:res.data.is_examine,
          is_urgent:res.data.is_urgent,
          is_picc:res.data.is_picc
        })
      },
      fail: function (err) {
        console.log(err)
      },

    })
    that.getaJoblist()
    that.getHostleve()
    that.setData({
      zid: zid
    })
  },

  onShow: function () {
    var that = this
    let pages = getCurrentPages();
    console.log(that.data);
    let currPage = pages[pages.length - 1];
    console.log(currPage.data.agentname)
    if (currPage.data.hid && currPage.data.agentname) {
      that.setData({ //将携带的参数赋值
        hid: currPage.data.hid,
        agentname: currPage.data.agentname
      })
    }
    if (currPage.data.lat && currPage.data.lng) {
      that.setData({ //将携带的参数赋值
        lat: currPage.data.lat,
        lng: currPage.data.lng,
      })
    }

    if (currPage.data.introduce) {
      console.log(currPage.data.introduce)
      that.setData({ //将携带的参数赋值
        z_content: currPage.data.introduce,
      })
    }
    if (currPage.data.z_telephone) {
      that.setData({ //将携带的参数赋值
        z_telephone: currPage.data.z_telephone,
      })
    }
    if (currPage.data.parentid2) {
      var parentid = currPage.data.parentid2
      a.util.request({
        'url': 'entry/wxapp/index.ksbiaoqian',
        data: {
          id: parentid
        },
        success: function (res) {
          console.log(res)
          that.setData({
            authority: res.data.description
          })
        }
      })
      that.setData({ //将携带的参数赋值
        parentid: currPage.data.parentid2,
        z_room: currPage.data.z_room,
        name: currPage.data.name
      })
    }
  },

  getaJoblist() {
    var that = this
    a.util.request({
      'url': 'entry/wxapp/index.getjoblist',
      success: function (res) {
        console.log(res)
        that.setData({
          title: res.data
        })
      }
    })
  },
  mapViewTap() {
    var that = this
    wx.authorize({
      scope: 'scope.userLocation',
      complete: function (res) {
        console.log(res)
        wx.chooseLocation({
          success(str) {
            console.log(str)
            var latitude = str.latitude
            var longitude = str.longitude

            var key = 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM';
            //发送请求通过经纬度反查地址信息
            var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + str.latitude + "," + str.longitude + "&key=" + key + "&get_poi=1";
            wx.request({
              url: getAddressUrl,
              success: function (result) {
                console.log(result)
                var province = result.data.result.address_component.province;
                var city = result.data.result.address_component.city;
                var district = result.data.result.address_component.district;
                var address = result.data.result.address_component.province + '-' + result.data.result.address_component.city + '-' + result.data.result.address_component.district;
                console.log(address)
                that.setData({
                  address: address,
                  lng: longitude,
                  lat: latitude
                })
              }
            })
          }
        })
      }
    })
  },
  getHostleve() {
    var that = this
    a.util.request({
      'url': 'entry/wxapp/regin.jurisdiction',
      data: {},
      success: function (res) {
        console.log(res)
        var list = res.data
        that.setData({
         // fenleiList: list,
          fenleiIndex: 0
        })
      }
    })
  }
});